package com.cet.qz.unit01.chapter1_1;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;

import java.awt.*;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2020-12-20 21:38
 **/
public class Qz1_1_32 {

    /*
     *方法描述： 绘制坐标轴
     *
     * @param l 起点
     * @param r 终点
     * @param N 分段数
     * @return double[]
     * @author chenentao
     * @date 2020/12/21 10:39
     */
    public static double[] drawCoordinate(double l,double r,int N){

        StdDraw.setPenRadius(0.003);
        StdDraw.setPenColor(Color.BLUE);
        StdDraw.setXscale(l,r);
        StdDraw.setYscale(-1,N);
        StdDraw.line(l,0,r,0);

        double[] coordinates = new double[N + 1];
        coordinates[0] = l;
        double len = (r - l) / N;
        StdDraw.setPenRadius(0.005);
        StdDraw.setPenColor(Color.magenta);
        StdDraw.point(coordinates[0],0);
        for (int i = 1; i <= N; i++) {
            coordinates[i] = l + i*len;
            StdDraw.point(coordinates[i],0);
        }
        return coordinates;
    }

    /*
     *方法描述： 绘制直方图
     *
     * @param coordinates   坐标
     * @param whiteList     白名单
     * @return void
     * @author chenentao
     * @date 2020/12/21 10:40
     */
    public static void drawCubemap(double[] coordinates,double[] whiteList){
        int[] counts = new int[coordinates.length - 1];
        double interval = (coordinates[coordinates.length - 1] - coordinates[0]) / (coordinates.length -1);
        for (int i = 0; i < whiteList.length; i++) {
            double n = whiteList[i] / interval;
            if (n >= 0.0 && n < coordinates.length) {
                ++counts[(int)n];
            }
        }
        for (int i = 0; i < counts.length; i++) {
            StdDraw.filledRectangle(interval*(i+0.5),0.5*counts[i],0.3*interval,0.5*counts[i]);
        }
    }

    public static void main(String[] args) {
        double l = 0;
        double r = 10;
        int N = 10;

        In in = new In("src/com/cet/qz/data/whiteList-1-1-32.txt");
        double[] whiteList = in.readAllDoubles();
        double[] coordinates = drawCoordinate(l, r, N);
        drawCubemap(coordinates,whiteList);
    }
}
